﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BankCreditPortfolio.DAL.Services;
using FluentNHibernate.Mapping;
using NHibernate;
using FluentNHibernate.Data;

namespace BankCreditPortfolio.DAL.Entities
{
    public class OrderInfoPersistence
    {
        public virtual int OrderId { get; set; }
        public virtual decimal Gains { get; set; }
        public virtual decimal Amount { get; set; }
        public virtual string Documents { get; set; }
        //public virtual Guarantor Guarantor { get; set; }
        public virtual Currency GainsCurrency { get; set; }
        public virtual OrderStatus OrderStatus { get; set; }
        public virtual Currency Currency { get; set; }
        public virtual TariffPersistence Tariff {get; set;}
        public virtual Client Client { get; set; }
    }

    public class OrderInfo : OrderInfoPersistence
    {
        new public Tariff Tariff
        {
            get
            {
                return (Tariff)base.Tariff;
            }
            set
            {
                base.Tariff = value;
            }
        }

        public List<Guarantor> Guarantors
        {
            get
            {
                var query = (new GuarantorService()).CreateQuery().Where(x => x.OrderInfo.OrderId == this.OrderId);
                return query.Count() > 0 ? query.ToList() : new List<Guarantor>();
            }
        }
    }

    public class OrderMap : ClassMap<OrderInfoPersistence>
    {
        public OrderMap()
        {
            Id(x => x.OrderId).GeneratedBy.HiLo("orderinfo");
            Id(x => x.OrderId).Unique();
            Map(x => x.Gains);
            Map(x => x.Documents);
            Map(x => x.Amount);
            //References(x => x.Guarantor).Column("GuarantorId");
            References(x => x.OrderStatus).Column("OrderStatusId");
            References(x => x.Tariff).Column("TariffId");
            References(x => x.Client).Column("ClientId");
            References(x => x.GainsCurrency).Column("GainsCurrencyId");
            References(x => x.Currency).Column("CurrencyId");
            Table("OrderInfo");
        }
    }
}
